package com.weme.live.view

import android.animation.ValueAnimator
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.core.content.ContextCompat
import com.weme.common.util.gone
import com.weme.common.util.startCountdown
import com.weme.common.util.visible
import com.weme.common.dialog.RechargeDialog
import com.weme.common.dialog.RechargeDialogCallback
import com.weme.common.util.UtilsApp
import com.weme.common.util.showToast
import com.weme.live.databinding.ViewGuideConsumptionBinding
import com.weme.live.dialog.LivePayDialog
import dev.kratos.api.user_server.v1.GiftInfo

class GuideConsumptionView @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null, defStyle: Int = 0) : FrameLayout(context, attributeSet, defStyle) {
    private val viewBinding = ViewGuideConsumptionBinding.inflate(LayoutInflater.from(context), this, true)

    var animator = ValueAnimator()
    var isRecharge = false
    var dialog :LivePayDialog? = null

    init {
        dialog = LivePayDialog(UtilsApp.getTopActivity(),isRecharge)
        viewBinding.lrbTime.setOnClickListener {
            if (isRecharge) {
                RechargeDialog.show(context,object : RechargeDialogCallback {
                    override fun sendGiftInfo(giftInfo: GiftInfo) {

                    }

                })
            }else{
                context.showToast("跳转到订阅商城")
            }
        }
        animator = viewBinding.lrbTime.startCountdown(maxProgress = 30, // 倒计时总时长（秒）
            onProgressUpdate = { progress ->
                Log.e("live time",progress.toString())
                if(progress.toInt()==83){
                    if(!dialog!!.isShowing){
                        dialog?.show()
                    }
                }else if(progress.toInt() == 50){
                    if(!dialog!!.isShowing){
                        dialog?.show()
                    }
                }
                viewBinding.lrbTime.setProgress(progress)
            }, onComplete = {

            })
//        viewBinding.lrbTime.postDelayed({
//            try {
//                dialog = PayDialog(context,isRecharge)
//                dialog?.show()
//            }catch (e :Exception){
//
//            }
//
//        }, 5000)

    }

    fun getGuideConsumptionView(): LiveProgressBar {
        return viewBinding.lrbTime
    }

    fun setTypeUi(isRecharge: Boolean) {
        this.isRecharge = isRecharge
        if (isRecharge) {

            viewBinding.groupRecharge.visible()
            viewBinding.groupView.gone()
        } else {
            viewBinding.groupRecharge.gone()
            viewBinding.groupView.visible()
            viewBinding.lrbTime.mClipRoundBackgroundPaint.color = ContextCompat.getColor(context,com.example.resources.R.color.app_btn_colors)
        }
    }

    fun onDestruction() {
        animator.clone()
    }
}